package net.kts.attraction.subway.smallpass;



import java.util.List;
import java.util.Map;

import net.kts.attraction.subway.smallpass.model.infomation.MultiLineStation;
import net.kts.attraction.subway.smallpass.model.infomation.MultiLineTime;
import net.kts.attraction.subway.smallpass.model.infomation.StationInfo;
import net.kts.attraction.subway.smallpass.model.infomation.StationTransfer;
import net.kts.attraction.subway.smallpass.model.infomation.StationTransferTime;
import net.kts.attraction.subway.smallpass.process.SmallPassSubway;
import net.kts.attraction.subway.smallpass.process.check.MultiLineChecker;
import net.kts.attraction.subway.smallpass.process.check.SmallWayChecker;
import net.kts.attraction.subway.smallpass.process.check.StationLineChecker;
import net.kts.attraction.subway.smallpass.process.check.TimeChecker;
import net.kts.attraction.subway.smallpass.sqlite.SubwayDaoSQLite;
import net.kts.define.SQLiteDatabaseStr;
import net.kts.util.sqlite.MoveDatabase;
import android.app.Activity;
import android.app.ProgressDialog;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;


public class SmallSubwayStartActivity extends Activity{
	
	// 지하철 갈래길 소요시간     // key, value = 갈래길 역코드 , 갈래길 소요시간 model
	private static Map<Integer, List<MultiLineTime>> multiLineTimeMap = null;
	// 갈래길 정보    // key, value= 갈래길 역코드 , 갈래길 정보 model
	private static Map<Integer, MultiLineStation> multiLineStationMap = null;

	// 환승역 정보들
	private static Map<Integer, StationTransfer> stationTransferMap = null;

	// 환승 시간 정보  key, value = stationCd, 환승역 소요시간정보
	private static Map<Integer, StationTransferTime> stationTransferTimeMap = null;

	// 역정보  key, value = 역코드 , 역정보
	private static Map<Integer, StationInfo> stationInfoMap = null;

	public static ProgressDialog progressDialog = null;
	

	private SmallPassSubway smallPassSubway = null;
	
	private boolean startFlag = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		this.startFlag = true;
		
		SmallSubwayStartActivity.progressDialog = new ProgressDialog(this);
		SmallSubwayStartActivity.progressDialog.setMessage("search Small Subway...");
		SmallSubwayStartActivity.progressDialog.show();
		
	}
	
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		
		
		init();
		
		Intent intent = getIntent();
		
		Bundle bundle = intent.getExtras();
		
		int startStationCode = (Integer) bundle.get("startCode");
		int endStationCode = (Integer)bundle.get("endCode");
		
		if(startFlag) {
			this.startFlag = false;
			SmallWayChecker.startFlag = true;
//			SmallWayChecker.executorService.shutdown();
			
			SmallWayChecker.setGlastPath(null);
			this.smallPassSubway.goSmallWay(startStationCode, endStationCode);
		}
		else {
			finish();
		}
		
		
	}

	




	private  void init() {
    	//SQLiteDatabase.openDatabase("data/data/net.asinayo.sqlite/databases/kts.db", null, SQLiteDatabase.OPEN_READONLY);
    	
//    	this.daoSqlite = new InitDaoSqlite(this);
//    	
//    	this.daoSqlite.testMultiLine();
//    	String pakageName = "net.asinayo.sqlite";
//    	String dbFileName = "kts001.db";
//    	String assertPath = "koreaTravelStory.db3";
		
		SubwayDaoSQLite daoSQLite = null;
		
    	if(SmallSubwayStartActivity.stationInfoMap == null || SmallSubwayStartActivity.stationTransferMap == null || SmallSubwayStartActivity.stationTransferTimeMap == null
    			|| SmallSubwayStartActivity.multiLineStationMap == null || SmallSubwayStartActivity.multiLineTimeMap == null) {
    		MoveDatabase md = new MoveDatabase(this, SQLiteDatabaseStr.pakageName, SQLiteDatabaseStr.dbFileName, SQLiteDatabaseStr.assertPath);
    		md.assertToData();
    		
    		daoSQLite = new SubwayDaoSQLite(this, SQLiteDatabaseStr.dbFileName, null, 1);
    		
    		daoSQLite.defaulmultiLineTime();
    		daoSQLite.defaultMultiLineSation();
    		daoSQLite.defaultTransferinfo();
    		daoSQLite.defaultStationTransferTime();
    		daoSQLite.defaultStationInfos();
    		
    		SmallSubwayStartActivity.multiLineStationMap = daoSQLite.getMultiLineStationMap();
    		SmallSubwayStartActivity.multiLineTimeMap = daoSQLite.getMultiLineTimeMap();
    		SmallSubwayStartActivity.stationInfoMap = daoSQLite.getStationInfoMap();
    		SmallSubwayStartActivity.stationTransferMap = daoSQLite.getStationTransferMap();
    		SmallSubwayStartActivity.stationTransferTimeMap = daoSQLite.getStationTransferTimeMap();
    	}
    	
		
		
		smallPassSubway = new SmallPassSubway(SmallSubwayStartActivity.multiLineTimeMap, SmallSubwayStartActivity.multiLineStationMap, SmallSubwayStartActivity.stationTransferMap, SmallSubwayStartActivity.stationTransferTimeMap, SmallSubwayStartActivity.stationInfoMap);
		
		// ���� ���� ������ ȣ������ üũ�� �ϴ� ��ü
		SmallWayChecker.stationLineChecker = new StationLineChecker(SmallSubwayStartActivity.stationTransferMap);
		// ���� ���� �������� �߽����� �˻��ϴ� ��ü
		SmallWayChecker.multiLineChecker = new MultiLineChecker(SmallSubwayStartActivity.multiLineStationMap);
		
		// ������, ȯ�� �ð� ���ϴ� Ŭ����
		SmallWayChecker.timeChecker = new TimeChecker(SmallSubwayStartActivity.multiLineTimeMap, SmallSubwayStartActivity.stationTransferTimeMap);
		
		SmallWayChecker.uiCallBack = this.uiCallBack;
		
		if(daoSQLite != null) {
			daoSQLite.close();
		}
		
		
		// õȣ - ��� : 2548 - 216
		// õȣ - �����伺 : 2548 - 2814
		// ������ - ���� : 2547 - 2522
		// ���� - �浿 2554 - 2550
		// �ɵ��� , �ѽŴ��Ա� : 234 - 432
		// �ҿ��, �渶��� : 1916 - 1451
		// �ҿ��, ��ũ�� ��ũ : 1916 - 3134 -> ����
		// ���ϵ�, ��ȭ : 2554 - 2511
		// �����ٸ�, ������ : 2551 - 2546
		// ����, ������ : 2549, 2546
		// �浿 , ��ʸ� : 2549 - 2543
		// ��ȭ��, ���� : 2534 - 2547
		// ��ȭ , ���ϵ� : 2511 - 2554
		
//		Log.d("kts", "start goSmallWay");
		
		
//		SmallWayChecker.progressDialog = ProgressDialog.show(getBaseContext(), "Processing....", "Fast Line Search");
		
		
		
//		Log.d("kts", "end goSmallWay");
    	
    	// �⺻ ���� model ä���
		
		
//		Intent intent = new Intent(this, TempActivity.class);
//		
//		startActivity(intent);
    	
    }
    
    private Handler uiCallBack = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			
			Intent intent = new Intent(getBaseContext(), SmallSubwayResultActivity.class);
					
			startActivity(intent);
			
		}
    	
    };

}
